package com.longy.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.longy.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.longy.gulimall.product.entity.AttrEntity;
import com.longy.gulimall.product.service.AttrAttrgroupRelationService;
import com.longy.gulimall.product.service.AttrService;
import com.longy.gulimall.product.service.CategoryService;
import com.longy.gulimall.product.vo.AttrGroupRelationVo;
import com.longy.gulimall.product.vo.AttrGroupWithAttrsVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.longy.gulimall.product.entity.AttrGroupEntity;
import com.longy.gulimall.product.service.AttrGroupService;
import com.longy.common.utils.PageUtils;
import com.longy.common.utils.R;


/**
 * 属性分组
 *
 * @author longyang
 * @email longyang3240@163.com
 * @date 2020-11-14 16:27:07
 */
@Slf4j
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;


    ///product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrWithattr(@PathVariable Long catelogId){

        //根据三级分类id查询所有分组
        //根据分组信息查出所有属性
        List<AttrGroupWithAttrsVo> list = attrGroupService.getAttrWithattrByCatelogId(catelogId);
        return R.ok().put("data", list);
    }

    ///product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> attrGroupIds){
        relationService.saveBatch(attrGroupIds);
        return R.ok();
    }

    ///product/attrgroup/{attrgroupId}/noattr/relation
    /**
     * 获取分组id中没有关联的属性
     * @param attrgroupId
     * @param params
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page  = attrService.attrNoRelation(attrgroupId,params);
        return R.ok().put("page",page);
    }


    ///product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteAttrRelation(vos);
        return R.ok();
    }


    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable Long attrgroupId){
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable Integer catId) {
        if (catId == 0) {
            PageUtils page = attrGroupService.queryPage(params);
            return R.ok().put("page", page);
        }else {
            PageUtils page = attrGroupService.queryPage(params, catId);
            return R.ok().put("page", page);
        }
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
//        attrGroupId = 225L;
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();

        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
